import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.Set;

public class RedisTest {
    public static void main(String[] args) {
        //连接Redis服务器
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {

        }
    }

    //set和get
    public static void testGetSet(Jedis jedis){
        jedis.set("k1", "v1");
        jedis.set("k2", "v2");

        System.out.println(jedis.get("k1"));
        System.out.println(jedis.get("k2"));
    }

    //exists和del
    public static void testExistsDel(Jedis jedis){
        jedis.set("k1", "v1");
        jedis.set("k2", "v2");

        System.out.println(jedis.exists("k1"));
        System.out.println(jedis.exists("k2"));
        System.out.println(jedis.exists("k3"));

        jedis.del("k1");
        jedis.del("k2");

        System.out.println(jedis.exists("k1"));
        System.out.println(jedis.exists("k2"));
    }

    //keys
    public static void testKeys(Jedis jedis){
        jedis.set("k1", "v1");
        jedis.set("k2", "v2");
        jedis.set("k3", "v3");

        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
    }

    //expire 和 ttl
    public static void testExpireTtl(Jedis jedis){
        jedis.set("k1", "v1");
        jedis.set("k2", "v2");
        jedis.set("k3", "v3");

        jedis.setex("k4", 10, "v4");

        jedis.expire("k1", 10);
        jedis.expire("k2", 10);
        jedis.expire("k3", 10);

        long k4 = jedis.ttl("k4");

        System.out.println(k4);
        System.out.println(jedis.ttl("k2"));
        System.out.println(jedis.ttl("k3"));
    }

    //type
    public static void testType(Jedis jedis){
        jedis.set("k1","v1");
        System.out.println(jedis.type("k1"));
        jedis.hset("k2","k21","v21");
        System.out.println(jedis.type("k2"));
        jedis.lpush("k3","v31","v32","v33");
        System.out.println(jedis.type("k3"));
        jedis.sadd("k4","v41","v42","v43");
        System.out.println(jedis.type("k4"));
        jedis.zadd("k5",1,"v51");
        System.out.println(jedis.type("k5"));
    }

    //mget 和 mset
    public static void testMgetMset(Jedis jedis){
        jedis.mset("k1","v1","k2","v2","k3","v3");
        System.out.println(jedis.mget("k1","k2","k3"));
    }

    //append
    public static void testAppend(Jedis jedis){
        jedis.set("k1","v1");
        jedis.append("k1","v2");
        System.out.println(jedis.get("k1"));
    }

    //getrange 和 setrange
    public static void testGetRangeAndSetRange(Jedis jedis) {
        jedis.set("key", "abcdefg");
        String value = jedis.getrange("key", 1, 4);
        System.out.println(value);
        jedis.setrange("key", 0, "xyz");
        value = jedis.get("key");
        System.out.println(value);
    }

    //setnx
    public static void testSetnx(Jedis jedis){
        jedis.setnx("k1","v1");
        jedis.setnx("k1","v2");
        System.out.println(jedis.get("k1"));
    }

    //psetex
    public static void testPsetex(Jedis jedis){
        jedis.psetex("k1",1000,"v1");
        System.out.println(jedis.get("k1"));
    }

    //incr 和 decr
    public static void testIncrAndDecr(Jedis jedis){
        jedis.set("k1","10");
        jedis.incr("k1");
        System.out.println(jedis.get("k1"));
        jedis.decr("k1");
        System.out.println(jedis.get("k1"));
    }

    //incrby 和 decrby
    public static void testIncrbyAndDecrby(Jedis jedis){
        jedis.set("k1","10");
        jedis.incrBy("k1",5);
        System.out.println(jedis.get("k1"));
        jedis.decrBy("k1",5);
        System.out.println(jedis.get("k1"));
    }

}
